/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Dsrv6.bl;

import captions.CaptionsEnum;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import Dsrv6.persistence.controllers.AeeRegistriesJpaController;
import Dsrv6.persistence.controllers.RegisterJpaController;
import Dsrv6.persistence.entities.AeeRegistries;
import Dsrv6.persistence.entities.Register;
import kimo.persistence.controllers.AdtTypeJpaController;
import kimo.persistence.controllers.SexJpaController;
import kimo.persistence.entities.AdtType;
import kimo.persistence.entities.Producer;
import kimo.persistence.entities.Sex;

/**
 *
 * @author a381u296
 */
public class Dsrv6Person {

    private String afm;

    private EntityManagerFactory emf = Persistence.createEntityManagerFactory("Dsrv6DBPU");

    private RegisterJpaController rjc = new RegisterJpaController(emf);
    private AeeRegistriesJpaController arjc = new AeeRegistriesJpaController(emf);

    private EntityManagerFactory kimoEmf = Persistence.createEntityManagerFactory("DSrvKimoPU") ;
    private AdtTypeJpaController atjc = new AdtTypeJpaController(kimoEmf);
    private SexJpaController sjc = new SexJpaController(kimoEmf);
    
    private Register register;

    private AeeRegistries aeeRegistries;

    private Boolean registerExist;
    private Boolean registerFarmer;

    private Boolean aeeRegistriesExist;
    private Boolean aeeRegistriesFarmer;

    private Producer producer;

    private CaptionsEnum message;

    public CaptionsEnum getMessage() {
        return message;
    }

    public Producer aeeRegistriesToProducer(AeeRegistries aeeRegistries) {
        Producer p = new Producer();
        p.setProducerAfm(aeeRegistries.getAfm());
        p.setProducerSurname(aeeRegistries.getLastname());
        p.setProducerFirstName(aeeRegistries.getFirstname());
        p.setProducerFatherName(aeeRegistries.getFathername());
        p.setProducerBirthdate(aeeRegistries.getDtebirth());
        // p.setProducerSexId(new Sex()); // δεν δινει sex το CD - 
        AdtType adtType = atjc.findAdtType(aeeRegistries.getIdtype().intValue());
        p.setProducerAdtTypeId(adtType);
        p.setProducerAdt(aeeRegistries.getIdno());
        p.setProducerAdtIssueDate(aeeRegistries.getIddteekd());
        p.setProducerAdtIssueDepartment(aeeRegistries.getIdekdarxh());
        return p;
    }

    public Producer registerToProducer(Register register) {
        Producer p = new Producer();
        p.setProducerAfm(register.getTaxId());
        p.setProducerSurname(register.getSurname());
        p.setProducerFirstName(register.getFirstname());
        p.setProducerFatherName(register.getFathername());
        p.setProducerBirthdate(register.getBirthDate());
        Sex sex = sjc.findSex(register.getGndGndId().getGndId().intValue());
        p.setProducerSexId(sex);
        //DiscountCode discountCode = new DiscountCodeJpaController().findDiscountCode('H');
        AdtType adtType = atjc.findAdtType(register.getIdtIdtId().getIdtId().intValue());
        p.setProducerAdtTypeId(adtType);
        p.setProducerAdt(register.getIdNo());
        p.setProducerAdtIssueDate(register.getIdDate());
        p.setProducerAdtIssueDepartment(register.getIdPlace());
        return p;
    }

    public Dsrv6Person(String afm) {
        this.afm = afm;
    }

    public String getAfm() {
        return afm;
    }

    public Boolean isRegisterExist() {
        return registerExist;
    }

    public Boolean isRegisterFarmer() {
        return registerFarmer;
    }

    public Boolean isAeeRegistriesExist() {
        return aeeRegistriesExist;
    }

    public Boolean isAeeRegistriesFarmer() {
        return aeeRegistriesFarmer;
    }

    public Producer FindAFM() {

        try {
            register = (Register) rjc.getEntityManager()
                    .createNamedQuery("Register.findByTaxId")
                    .setParameter("taxId", afm) // 
                    .getSingleResult();
        } catch (Exception e) {
            register = null;
        }

        if (register != null) {
            registerExist = true;
            registerFarmer = register.getIsFarmer();
        } else {
            registerExist = false;
            registerFarmer = false;
        }

        if (registerExist && registerFarmer) {
            producer = registerToProducer(register);
            message = CaptionsEnum.AFM_EXIST;
        }

        try {
            aeeRegistries = (AeeRegistries) arjc.getEntityManager()
                    .createNamedQuery("AeeRegistries.findByAfm")
                    .setParameter("afm", afm) // 
                    .getSingleResult();
        } catch (Exception e) {
            aeeRegistries = null;
        }

        if (aeeRegistries != null) {
            aeeRegistriesExist = true;
            aeeRegistriesFarmer = aeeRegistries.getAgrotisflag();
        } else {
            aeeRegistriesExist = false;
            aeeRegistriesFarmer = false;
        }

        if (registerExist && !registerFarmer && aeeRegistriesExist && aeeRegistriesFarmer) {
            message = CaptionsEnum.AFM_NEED_VALIDATION;
            producer = aeeRegistriesToProducer(aeeRegistries);
        }

        if (!registerExist && !registerFarmer && aeeRegistriesExist && aeeRegistriesFarmer) {
            // AITHSH ME MYNHMA ME PRODUCER ΑΠΟ AEEREGISTERRIES
            message = CaptionsEnum.AFM_NEED_VALIDATION;
            producer = aeeRegistriesToProducer(aeeRegistries);
        }

        if ((registerExist && !registerFarmer && aeeRegistriesExist && !aeeRegistriesFarmer)
                || (registerExist && !registerFarmer && !aeeRegistriesExist && !aeeRegistriesFarmer)
                || (!registerExist && !registerFarmer && aeeRegistriesExist && !aeeRegistriesFarmer)) {
            message = CaptionsEnum.AFM_NOT_FARMER;
            
        }

        if (!registerExist && !aeeRegistriesExist) {
            message = CaptionsEnum.AFM_NOT_FOUND;
        }

        return producer;

    }

}
